package JavaSE.CODE.javaCollection;

import java.util.Objects;

public class Student {
    int age = 10;
    String name = "JavaSE/CODE";

    public Student(){}

    public Student(String name, int age){
        this.age = age;
        this.name = name;
    }

    public int getAge() {
        return age;//不用显示声明this
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return
                "age:" + age +
                ", name:'" + name
                ;
    }


//    public boolean equals(Object o) {
//        if (this == o) return true;
//        if (o == null || getClass() != o.getClass()) return false;
//        Student student = (Student) o;
//        return age == student.age && Objects.equals(name, student.name);
//    }


    @Override
    public int hashCode() {
        return age*31+name.hashCode();
    }

    public boolean equals(Object obj){
        //1、判断是否为空
        if(obj==null) return false;
        //2、判断是否是同一个元素
        if(this==obj) return true;
        //3、判断是否为同一类型
        if(obj instanceof Student){
            //如果是同一类型，强转
            Student student = (Student) obj;
            //4、比较属性
            if(this.name.equals(student.getName())&&this.age==student.getAge())
                return true;

        }
        //5、不是同一类型，或者属性不相等，就返回false
        return false;
    }
}
